package com.example.demo;


import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.example.common.AjaxJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

//http://127.0.0.1:7082/test/test/a
//周润发
@Api(tags = "质检")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    ZhiService service;

//    @RequestMapping(value = "/a", method = RequestMethod.GET)
//    public String selectList() {
//        ExcelUtil.readBySax("d:\\111.xls", 0, createRowHandler1());
//        ExcelUtil.readBySax("d:\\111.xls", 1, createRowHandler2());
//        return "";
//    }

    public RowHandler createRowHandler1() {
        return new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
            }
        };
    }
    public  RowHandler createRowHandler2() {
        return new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
            }
        };
    }
    @ApiOperation("质检exec")
    @GetMapping("/zhiJian")
    public AjaxJson zhiJian(String exec,String path,
                            @RequestParam(required = false,defaultValue = "0") Integer type){
        AjaxJson ajaxJson = null;
        Path path1 = Paths.get(path);
        boolean directoryExists = Files.exists(path1) && Files.isDirectory(path1);
        Path paths = Paths.get(exec);

        if (!Files.exists(paths)){
            return AjaxJson.returnExceptionInfo("exec路径错误");
        }
        if (!directoryExists){
            return AjaxJson.returnExceptionInfo("图片路径错误");
        }
        try {
            ajaxJson = service.ExcelUtil4(exec,path,type);
        } catch (Exception e) {
           return AjaxJson.returnInfo("质检错误"+e);
        }
        return ajaxJson;
    }

}
